#!/bin/sh 
#JAVA_HOME=/usr/local/jdk
#echo $JAVA_HOME

APP_HOME=/home/saitron/java/ky/api
APP_NAME=saitron-zhky-provider-zhky-1.3.2


JAVA_OPTS=" -XX:+AggressiveOpts -XX:+UseParallelGC  -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails"

check_env()
{
	if [ -n "$JAVA_HOME" ]; then
		echo ""
		echo "====================================================="
		echo ""
		echo "=============       Java Version Check     =========="
		echo "Using java in " $JAVA_HOME 
		java -version
		echo "====================================================="
		echo ""
	else
		echo "Could not find JAVA_HOME, please install JDK first."
		exit -1
	fi
}
  
case "$1" in
  
  start)
    #check_env
	
    nohup java  $JAVA_OPTS -jar  $APP_HOME/$APP_NAME.jar >>./logs/java.log 2>&1 &
	echo $! > $APP_HOME/$APP_NAME.pid
	echo "$APP_NAME has been started"
	;;  

  stop)
    #check_env
	
    #kill `cat $APP_HOME/$APP_NAME.pid`
    rm -rf $APP_HOME/$APP_NAME.pid

    pid=$(ps -ef | grep 'jav[a]' | grep "$APP_NAME" | awk '{ print $2 }')
    if [ ! -z "$pid" ]; then
    arr=(`echo $pid | tr ',' ' '`) 
    for s in ${arr[@]}
    do
       kill $s
    done
    fi

    echo "$APP_NAME has been stopped"
    ;;  

  restart)
    $0 stop
    sleep 1
    $0 start
    ;;

  *)  
    echo "Usage: run.sh {start|stop|restart}"
    ;;

esac

exit 0

